{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "e0269d59-b1c1-4cb5-a80d-67beb34e7f80",
   "metadata": {},
   "outputs": [],
   "source": [
    "#!/usr/bin/python\n",
    "# -*- coding: UTF-8 -*-\n",
    "# author : Liu Kun\n",
    "# date   : 2021-08-29 23:31:55\n",
    "\n",
    "import os\n",
    "from minio import Minio\n",
    "from kit_config import Config\n",
    "\n",
    "class MinIO(object):\n",
    "    \n",
    "    __client = None\n",
    "    \n",
    "    def __init__(self, host, port, access_key, secret_key):\n",
    "        self.__client = Minio(f\"{host}:{port}\", access_key=access_key, secret_key=secret_key, secure=False)\n",
    "        pass\n",
    "    \n",
    "    def upload(self, fr, to, bucket):\n",
    "        if not os.access(fr, os.F_OK):\n",
    "            return None\n",
    "        if not self.__client.bucket_exists(bucket):\n",
    "            self.__client.make_bucket(bucket)\n",
    "        result = self.__client.fput_object(bucket, to, fr)\n",
    "        print(\"created {0} object; etag: {1}, version-id: {2}\".format(\n",
    "            result.object_name, result.etag, result.version_id,),)\n",
    "        pass\n",
    "    \n",
    "    def download(self, fr, to, bucket):\n",
    "        if os.access(to, os.F_OK):\n",
    "            os.remove(to)\n",
    "        self.__client.fget_object(bucket, fr, to)\n",
    "        pass\n",
    "    \n",
    "    def remove(self, target, bucket):\n",
    "        self.__client.remove_object(bucket, target)\n",
    "        pass\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    config = Config('../resources/config.yaml')\n",
    "    conf = config.get_object_storage('ds218-plus-test')\n",
    "    inst = MinIO(conf[\"host\"], conf[\"port\"], conf[\"access-key\"], conf[\"secret-key\"])\n",
    "    # inst.upload(\"./kit_minio.py\", \"kit_minio.py\", \"debug\")\n",
    "    # inst.download(\"kit_minio.py\", \"kit_minio_copy.py\", \"debug\")\n",
    "    # inst.remove(\"kit_minio.py\", \"debug\")\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4f79a14b-5f61-4bb1-95c8-26a8845eb5ac",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
